{#
 # ---------------------------------------------------------------------
 #
 # GLPI - Gestionnaire Libre de Parc Informatique
 #
 # http://glpi-project.org
 #
 # @copyright 2015-2025 Teclib' and contributors.
 # @copyright 2003-2014 by the INDEPNET Development Team.
 # @licence   https://www.gnu.org/licenses/gpl-3.0.html
 #
 # ---------------------------------------------------------------------
 #
 # LICENSE
 #
 # This file is part of GLPI.
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # the Free Software Foundation, either version 3 of the License, or
 # (at your option) any later version.
 #
 # This program is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 # GNU General Public License for more details.
 #
 # You should have received a copy of the GNU General Public License
 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
 #
 # ---------------------------------------------------------------------
 #}

{# @var \Glpi\Form\Form form #}
{# @var \Glpi\Form\AccessControl\FormAccessControl[] access_controls #}
{# @var string[] warnings #}

<div class="py-2 px-3">
    <div class="col-12 col-lg-6">
        <form
            method="POST"
            action="{{ path('/front/form/access_control.form.php') }}"
            data-track-changes="true"
            data-submit-once="true"
        >
            {# Display warnings at the top of the tab #}
            {% if warnings|length > 0 %}
                <div class="mb-5">
                    {% for warning in warnings %}
                        <div
                            class="alert alert-warning d-flex align-items-center"
                            role="alert"
                        >
                            <i class="ti ti-alert-triangle me-2"></i>
                            {{ warning }}
                        </div>
                    {% endfor %}
                </div>
            {% endif %}

            {# Display each access control policies #}
            {# Display each access control policies #}
            {% for access_control in access_controls %}
                {% set strategy = access_control.getStrategy() %}
                {% set config = access_control.getConfig() %}

                <section
                    class="mb-5"
                    style="{{ access_control.fields.is_active == false ? "opacity: 0.5" : "" }}"
                    {# Disabled item are showed with a lower opacity #}
                    data-glpi-toggle-control-target
                    data-glpi-access-control-form
                    aria-label="{{ strategy.getLabel() }}"
                >
                    <h3 class="d-flex align-items-center">
                        <i class="{{ strategy.getIcon() }} me-2"></i>
                        {{ strategy.getLabel() }}
                        <label class="form-check mb-0 ms-auto form-switch">
                            <input
                                type="hidden"
                                value="0"
                                name="{{ access_control.getNormalizedInputName("is_active") }}"
                            >
                            <input
                                aria-label="{{ __("Active") }}"
                                data-glpi-toggle-control
                                class="form-check-input"
                                type="checkbox"
                                name="{{ access_control.getNormalizedInputName("is_active") }}"
                                value="1"
                                {{ access_control.fields.is_active == true ? "checked" : "" }}
                            >
                        </label>
                    </h3>
                    <div>
                        {# Render custom config form #}
                        {{ strategy.renderConfigForm(access_control)|raw }}
                    </div>
                </section>
            {% endfor %}

            {% if form.canUpdate() and form.canUpdateItem() %}
                {# Submit button #}
                <div class="d-flex flex-row-reverse">
                    {# This button will submit the others forms using a custom script #}
                    <button
                        data-glpi-submit-id="access-controls"
                        type="submit"
                        class="btn btn-primary"
                        name="update"
                    >
                        <i class="ti ti-device-floppy me-2"></i>
                        {{ __("Save changes") }}
                    </button>
                </div>
            {% endif %}

            <input type="hidden" name="id" value="{{ form.fields.id }}"/>
            <input type="hidden" name="_glpi_csrf_token" value="{{ csrf_token() }}" />
        </form>
    </div>
</div>

<script>
    // Toggle disabled state
    $("[data-glpi-toggle-control]").on("change", function() {
        $(this).closest("[data-glpi-toggle-control-target]")
            .css("opacity", this.checked ? 1 : 0.5)
        ;
    });

    // Toggle state if any input is modified
    $("section[data-glpi-access-control-form] :input").on("change", function(e) {
        // Do not trigger on this is_active checkbox itself
        if ($(this).data("glpi-toggle-control") !== undefined) {
            return;
        }

        $(this).closest("section")
            .find("[data-glpi-toggle-control]")
            .prop("checked", true)
            .trigger("change")
        ;
    });
</script>
